package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Record;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.RedisKit;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.LuojwyServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel="luojwy")
public class LuojwyAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(LuojwyAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String size = orderBean.getProductCode();
		String mobile = orderBean.getPhoneNo();
		String tradeNo = orderBean.getIds();//订购流水号
		String notifyUrl =BetweenProperties.CALLBACK_URL+"Luojwy";
		String range = "";
		String region = orderBean.getRegion();
		
		if ("0".equals(region)) {
			range = "2";//上游的统付 对应 我们的 全国 0
		}else {
			String vasProd = RedisKit.get("vasproduct_"+orderBean.getVasProdId());
			Record vasProdRecord = new Gson().fromJson(vasProd,Record.class);
			log.info("产品信息["+vasProdRecord+"],该订单号为："+tradeNo);
            if (null !=vasProdRecord) {
            	 region = vasProdRecord.getStr("region");
     			if ("0".equals(region)) {//我们的省漫
     				range = "0";
     			}else if ("1".equals(region)) {
     				range = "1";
     			}else{//我们的省内
     				
     			}
			}
		   
		}
	
		String result = LuojwyServiceImpl.service.order(size,mobile,tradeNo,notifyUrl,range);
	
		JSONObject json = JSONObject.fromObject(result);

		int code = json.getInt("code");//状态码. 0: 充值中; -1: 下单失败

		if ("0".equals(String.valueOf(code))) {
			
			orderBean.setStatus("10");
			orderBean.setMessage(json.getString("info"));
			orderBean.setErrorMsg(json.getString("info"));
		} else {
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("info"));
			orderBean.setErrorMsg(json.getString("info"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	
}
